package com.dd.ent.car.common.http;

import android.graphics.Bitmap;
import android.view.View;
import android.widget.ImageView;

import com.dd.ent.car.R;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;

/**
 * Created by aqu-spring on 16/8/21.
 */
public class ImageLoaderUtil {

    private static DisplayImageOptions createAuthorDisplayImageOptions() {
        DisplayImageOptions.Builder builder = new DisplayImageOptions.Builder();
        builder.showImageOnLoading(R.drawable.default_user); // 设置在loading时的占位图
        builder.showImageForEmptyUri(R.drawable.default_user); // 设置图片为网络图片，而uri为空时的占位图
        builder.showImageOnFail(R.drawable.default_user); // 设置图片请求失败时的展位图
        builder.considerExifParams(true); // 设置是否考虑JPEG 类型图片的以下参数 如：rotate,
        builder.cacheInMemory(true); // 设置是否开启内存缓存
        builder.cacheOnDisk(true); // 设置是否开启磁盘缓存
        builder.displayer(new FadeInBitmapDisplayer(200));
        return builder.build();
    }


    private static DisplayImageOptions createPosterDisplayImageOptions() {
        DisplayImageOptions.Builder builder = new DisplayImageOptions.Builder();
        builder.showImageOnLoading(R.drawable.poster_icon_default); // 设置在loading时的占位图
        builder.showImageForEmptyUri(R.drawable.poster_icon_default); // 设置图片为网络图片，而uri为空时的占位图
        builder.showImageOnFail(R.drawable.poster_icon_default); // 设置图片请求失败时的展位图
        builder.considerExifParams(true); // 设置是否考虑JPEG 类型图片的以下参数 如：rotate,
        builder.cacheInMemory(true); // 设置是否开启内存缓存
        builder.cacheOnDisk(true); // 设置是否开启磁盘缓存
        builder.displayer(new FadeInBitmapDisplayer(200));
        return builder.build();
    }

    public static void displayPosterImage(String url, ImageView imageView) {
        displayImage(url,imageView,createPosterDisplayImageOptions());

    }

    public static void displayImage(String url, ImageView imageView) {
        displayImage(url, imageView, createAuthorDisplayImageOptions());
    }


    public static void displayImage(final String url, final ImageView imageView, DisplayImageOptions options) {
        if (imageView == null) {
            throw new NullPointerException("imageView is null");
        }
        int realKey = imageView.getId();
        if (realKey <= 0) {
            realKey = R.mipmap.ic_launcher;
        }
        final int realTagKey = realKey;
        Object prevUrlObj = imageView.getTag(realKey);
        if (prevUrlObj != null && prevUrlObj instanceof String) {
            String prevUrl = (String) prevUrlObj;
            if (prevUrl.equals(url)) {
                return;
            }
        } else {
            ImageLoader.getInstance().cancelDisplayTask(imageView);
        }

        ImageLoader.getInstance().displayImage(url, imageView, options, new SimpleImageLoadingListener() {
            @Override
            public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                imageView.setTag(realTagKey, url);
            }
        });

    }

}
